<template>
    <el-container class="index-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../../static/img/logo.png" />
                <span>秒妙测后台管理系统</span>
            </div>
            <span>
                <span>用户：{{ username }}</span>
                <img :src="photo">
                <el-dropdown>
                    <span class="el-dropdown-link">
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="routerPath">用户信息</el-dropdown-item>
                        <el-dropdown-item @click.native="outlogin">退出</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </span>
        </el-header>
        <!-- 页面主题区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <!-- 三元表达式，当isCollapse为true时，width为64px,反之为200px -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened='true' :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="'/'+ item.path" v-for="(item,index) in menulist" :key="index">
                        <!-- 一级菜单模版区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{ item.name }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + item2.path" v-for="(item2,index) in item.children" :key="index" @click="saveNavState('/' + item2.path)">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{ item2.name }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主题 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
import { menu_list_get, before_each_get } from './axios_api/api'
import { config } from '../config'
export default {
    data() {
        return {
            menulist: [],
            username: localStorage.getItem('username'),
            photo: '',
            iconsObj: {
                '1': 'el-icon-pie-chart',
                '2': 'el-icon-tickets',
                '3': 'el-icon-s-promotion',
                '4': 'el-icon-postcard',
                '5': 'el-icon-user-solid',
                '6': 'el-icon-s-marketing',
                // '7': 'el-icon-message-solid',
            },
            // 是否折叠侧边栏
            isCollapse: false,
            // 激活的连接地址
            activePath: '',
        }
    },
    mounted() {
        this.get_menulist();
        this.activePath = localStorage.getItem('activePath')
        this.photo = config['uploadurl'] + localStorage.getItem('photo')
    },
    methods: {
        // 跳转到用户信息页面
        routerPath() {
            this.$router.push('/user_info')
        },
        // 保存连接的激活状态
        saveNavState(activePath) {
            localStorage.setItem('activePath', activePath)
            this.activePath = activePath
        },
        // 点击按钮，折叠侧边栏
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        },
        // 获取侧边栏标签
        get_menulist() {
            menu_list_get().then(resp => {
                this.menulist = resp.menulist
                // console.log(this.menulist)
            })
        },
        // 退出登录
        outlogin() {
            localStorage.removeItem('token')
            localStorage.removeItem('activePath')
            localStorage.removeItem('username')
            localStorage.removeItem('photo')
            localStorage.removeItem('pid')
            this.$router.push('/login')
            this.$message.info('退出成功')
        }
    }
}
</script>

<style lang="scss" scoped>
.index-container {
    height: 100%;
}
.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
        img {
            width: 130px;
        }
    }
    > span {
        display: flex;
        align-items: center;
        span {
            margin-right: 5px;
            font-size: 15px;
        }
        img {
            width: 40px;
            height: 40px;
            border-radius: 10px;
            margin-right: 5px;
        }
    }
}
.el-aside {
    background-color: #333744;
    .el-menu {
        border-right: none;
    }
}
.el-main {
    background-color: #eaedf1;
}
.iconfont {
    margin-right: 10px;
}
.toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.4em;
    cursor: pointer;
}
.el-icon--right {
    color: #fff;
    margin-right: -10px;
}
</style>
